<?php

/**
* alltosun.com 资讯控制器 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-8-8 下午5:08:33 $
* $Id$
*/

class Action
{
    private $per_page = 20;
    /**
     * 显示资讯列表
     */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);
        $type          = Request::Get('type', 3);
        $is_advenced   = Request::getParam('is_advenced', 0);
        $search_filter = Request::getParam('search_filter', array());

        // 拼装搜索条件
        $filter = array();
        if (isset($search_filter['title']) && $search_filter['title']) {
            $filter['`title` LIKE '] = '%'.$search_filter['title'].'%';
        }
        if (isset($search_filter['category_id']) && $search_filter['category_id']) {
            $filter['category_id'] = $search_filter['category_id'];
        }
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }
        $filter['status'] = $status;
        
        if (3 != $type) {
            $filter['type'] = $type;
        }
        
        $article_list = array();
        $article_count = _model('article')->getTotal($filter);
        if ($article_count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($article_count)) {
                Response::assign('pager', $pager);
            }

            $article_list = _model('article')->getList($filter, ' ORDER BY `view_order` DESC, `id` DESC '.$pager->getLimit($page));
        }
        foreach ($article_list as $k=>$v) {
            $article_list[$k]['link'] = AnUrl('article/help_detail?id='.$v['id']);
        }
        
        Response::assign('type', $type);
        Response::assign('article_list', $article_list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);
        Response::assign('is_advenced', $is_advenced);

        Response::display('admin/article_list.html');
    }

    /**
     * 添加资讯
     */
    public function add()
    {
        Response::display('admin/add.html');
    }

    public function test()
    {
        Response::display('admin/test.html');
    }

    /**
     * 编辑资讯
     */
    public function edit()
    {
        $id = Request::getParam('id', 0);

        $article_info = _uri('article', $id);

        Response::assign('article_info', $article_info);
        Response::assign('id', $id);

        Response::display('admin/add.html');
    }

    /**
     * 保存
     */
    public function save()
    {
        $info = Request::getParam('info', array());
        $id   = Request::getParam('id', 0);

        foreach ($info as $k=>$v) {
            if (!is_numeric($v)) {
                $info[$k] = trim(AnFilter::filter_string($v));
            }
        }

        if (!isset($info['title']) || !$info['title']) {
            return '请填写短标题';
        }

        if (!isset($info['type']) || !$info['type']) {
            return '请填写文章类型';
        }

        if ($info['alias']) {
            $old_info = _uri('article', array('alias'=>$info['alias']));

            if ($old_info && $old_info['id'] != $id) {
                return '该长标题已经存在';
            }
        }

        // @TODO 待定
//         if ($info['video_link']) {
//         	$str_left = '<video width="100%" autoplay="" height="100%" controls=""><source src="';
//         	$str_right = '" type="video/mp4"/></video>';
//         	$info['video_link'] = htmlspecialchars($str_left.$info['video_link'].$str_right);
//         }

        if ($id) {
            $article_info = _uri('article', $id);
            if (!$article_info) {
                return '该内容不存在';
            }

            _model('article')->update($id, $info);
        } else {
            _model('article')->create($info);
        }

        Response::redirect(AnUrl('article/admin'));
    }

    /**
     * 删除
     * @return array
     */
    public function delete()
    {
        $id = Request::getParam('id');

        if (!$id) {
            return '请选择要删除的内容';
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k=>$v) {
            if (!$v) {
                continue;
            }

            $article_info = _uri('article', (int)$v);
            if (!$article_info) {
                continue;
            }

            if (1 == $article_info['status']) {
                _model('article')->update($v, array('status'=>0));
            } else {
                _model('article')->delete($v);
            }
        }

        return 'ok';
    }

    /**
     * 更新状态
     * @return array
     */
    public function change_status()
    {
        $id     = Request::getParam('id', 0);
        $status = Request::getParam('status', 1);
        if (!$id) {
            return '请选择要更改的内容';
        }

        $article_info = _uri('article', $id);
        if ($status == $article_info['status']) {
            return '已经执行过此操作，不用重复提交';
        }

        if (1 == $status) {
            _model('article')->update($id, array('status'=>1));
        }

        return 'ok';
    }
    
    
    /**
     * 查看门店的设备绑定记录
     */
    
    
    
}
?>